package com.shanyue.redwine.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.shanyue.redwine.R;
import com.shanyue.redwine.databinding.FragmentWinecabinetOrderListBinding;
import com.shanyue.redwine.entity.Page;
import com.shanyue.redwine.entity.WineCabinetGoods;
import com.shanyue.redwine.entity.WineCabinetOrder;
import com.shanyue.redwine.http.ApiCallBack;
import com.shanyue.redwine.http.ApiResponse;
import com.shanyue.redwine.http.SendBase;
import com.shanyue.redwine.utils.Constants;
import com.shanyue.redwine.utils.RecyclerView.BaseViewHolder;
import com.shanyue.redwine.utils.RecyclerView.CommonAdapter;
import com.shanyue.redwine.utils.RecyclerView.CommonLoadMoreAdapter;
import com.shanyue.redwine.utils.Utils;
import com.shanyue.redwine.utils.recycleviewdivider.HorizontalDividerItemDecoration;
import com.shanyue.redwine.view.NoScrollRecyclerView;

import java.util.List;

/**
 * @ 创建 huangxiaoyu
 * @创建时间 2018/8/27 8:29
 * @描述 酒柜商品详情
 */
public class FragmentWineCabinetOrderList extends FragmentBase<FragmentWinecabinetOrderListBinding> {

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mPage = new Page();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_winecabinet_order_list;
    }

    @Override
    protected String getTitle() {
        return null;
    }

    @Override
    protected void initView(Bundle saveInstanceState) {
        mBinding.srlRefresh.setOnRefreshListener(() -> {
            mPage.setRefreshAble(true);
            mPage.setPageIndex(1);
            loadData();
        });
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager
                .VERTICAL, false);
        mBinding.rvList.setLayoutManager(linearLayoutManager);
        mBinding.rvList.addItemDecoration(new HorizontalDividerItemDecoration.Builder(getActivity()).sizeResId(R
                .dimen.dp10).colorResId(R.color.base_bg).build());
        mBinding.rvList.setAdapter(new CommonLoadMoreAdapter<WineCabinetOrder>(getActivity(), R.layout
                .item_winecabinet_order_list) {


            @Override
            protected void convert(BaseViewHolder holder, WineCabinetOrder item, int position) {
                holder.setText(R.id.tvNo, "商品编号:" + item.getEncode());
                holder.setText(R.id.tvAmount, "购买金额:" + item.getOrder_money());
                holder.setText(R.id.tvBuyTime, "购买时间:" + item.getCreate_date());
                NoScrollRecyclerView recyclerView = holder.getView(R.id.rvList);
                GridLayoutManager gridLayoutManager = new GridLayoutManager(getActivity(), 4);
                recyclerView.setLayoutManager(gridLayoutManager);
                recyclerView.setAdapter(new CommonAdapter<WineCabinetGoods>(getActivity(), R.layout
                        .item_winecabinet_order_list_goods, item.getOrder_goods()) {
                    @Override
                    protected void convert(BaseViewHolder subViewHolder, WineCabinetGoods item, int position) {
                        Utils.loadImg(getActivity(), item.getGoods_images(), subViewHolder.getView(R.id.ivIcon));
                    }
                });
            }

        });
        mBinding.rvList.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                LinearLayoutManager manager = (LinearLayoutManager) recyclerView.getLayoutManager();
                if (newState == RecyclerView.SCROLL_STATE_IDLE && manager.findLastVisibleItemPosition() >
                        recyclerView.getAdapter().getItemCount() - 5 && mPage.isRefreshAble()) {
                    mPage.setPageIndex(mPage.getPageIndex() + 1);
                    loadData();
                }
            }
        });
        mBinding.srlRefresh.post(() -> loadData());
    }

    void loadData() {
        JSONObject object = new JSONObject();
        object.put("page", mPage.getPageIndex());
        object.put("rows", mPage.getPageSize());
        SendBase sendBase = new SendBase(getActivity(), object);
        mRestClient.getRectService().getResultString(Constants.URL_WINE_CABINET_ORDER_LIST, JSON.toJSONString
                (sendBase, SerializerFeature.WriteSlashAsSpecial)).enqueue(new ApiCallBack<String>(getActivity()) {
            @Override
            protected void onResponse(ApiResponse<String> response) {
                String result = response.result;
                result.replace("\\", "");
                List<WineCabinetOrder> orderList = JSON.parseArray(result, WineCabinetOrder.class);
                if (orderList.size() < mPage.getPageSize()) {//灭有数据 不在加载更多
                    mPage.setRefreshAble(false);
                }
                if (mPage.getPageIndex() > 1) {
                    ((CommonLoadMoreAdapter) mBinding.rvList.getAdapter()).append(orderList);
                } else {
                    // TODO: 2018/8/29 提示空数据
                    ((CommonLoadMoreAdapter) mBinding.rvList.getAdapter()).refresh(orderList);
                }
                mBinding.srlRefresh.setRefreshing(false);
            }
        });

    }

    public static FragmentWineCabinetOrderList newInstance() {
        FragmentWineCabinetOrderList fragment = new FragmentWineCabinetOrderList();
        return fragment;
    }
}
